package cmpe451.group7.ourcity.entity;

import java.io.Serializable;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * Entity class for places.
 */
@Entity
@Table(name = "multimedia")
public class Multimedia implements Serializable {

	private static final long serialVersionUID = -2892229978903971102L;

	private long multimediaID;
	private User owner;
	private byte[] file;
	
	// for audio, URL is http://vocaroo.com/i/{path}
	private String path;
	
	private MultimediaType multimediaType;
	private String mimeType;
	private String title;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public long getMultimediaID() {
		return multimediaID;
	}

	public void setMultimediaID(long multimediaID) {
		this.multimediaID = multimediaID;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	@ManyToOne
	// (cascade = { CascadeType.PERSIST, CascadeType.MERGE,
	// CascadeType.REFRESH })
	// @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
	public User getOwner() {
		return owner;
	}

	public void setOwner(User owner) {
		this.owner = owner;
	}

	public MultimediaType getMultimediaType() {
		return multimediaType;
	}

	public void setMultimediaType(MultimediaType multimedia) {
		this.multimediaType = multimedia;
	}

	@Override
	public int hashCode() {
		return (multimediaID + path).hashCode();
	}

	public String getMimeType() {
		return mimeType;
	}

	public void setMimeType(String mimeType) {
		this.mimeType = mimeType;
	}

	@Lob
	@Basic(fetch = FetchType.LAZY)
	public byte[] getFile() {
		return file;
	}

	public void setFile(byte[] file) {
		this.file = file;
	}

	@Transient
	public boolean isPicture() {
		return multimediaType == MultimediaType.IMAGE;
	}

	@Transient
	public boolean isVideo() {
		return multimediaType == MultimediaType.VIDEO;
	}

	@Transient
	public boolean isAudio() {
		return multimediaType == MultimediaType.AUDIO;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String videoTitle) {
		this.title = videoTitle;
	}

	@Transient
	public String getEmbedURL() {
		if (path == null)
			return null;

		return path.replace("watch?v=", "embed/") + "?feature=oembed";
	}
}
